using System;
using Microsoft.SPOT;
using System.Collections;

namespace eHouse
{
    class LightsBL
    {
        private static LightsBL instance;        

        public static LightsBL GetInstance()
        {
            if(instance == null)
                instance = new LightsBL();

            return instance;
        }

        public bool[] GetLightsState()
        {
            bool[] stateArray = new bool[4];
            stateArray[0] = Drivers.getInstance().led_kitchen.Read();
            stateArray[1] = Drivers.getInstance().led_living.Read();
            stateArray[2] = Drivers.getInstance().led_bedroom.Read();
            stateArray[3] = Drivers.getInstance().led_bathroom.Read();

            return stateArray;
        }

        public void SetLight(string room, bool state)
        {
            switch (room)
            {
                case "kitchen": Drivers.getInstance().led_kitchen.Write(state); break;
                case "living": Drivers.getInstance().led_living.Write(state); break;
                case "bedroom": Drivers.getInstance().led_bedroom.Write(state); break;
                case "bathroom": Drivers.getInstance().led_bathroom.Write(state); break;
            }
        }

        public void SetAllLigths(bool state)
        {
            SetLight("kitchen", state);
            SetLight("living", state);
            SetLight("bedroom", state);
            SetLight("bathroom", state);
        }

        public ArrayList GetRules()
        {
            string rulesString = ConnectionManager.GetInstance().GetLightRules();
            rulesString = rulesString.Substring("RULES_LIGHT/".Length);
            string[] rules = rulesString.Split('|');
            ArrayList rulesList = new ArrayList();
           
            
            foreach (string rule in rules)
            {
                if (rule != "")
                {
                    Rule lightrule;
                    
                    string[] rule_parts = rule.Split('\\');
                    string id = rule_parts[0];                     
                    
                    string time = rule_parts[1];
                    string[] rooms = rule_parts[2].Split('/');
                    bool[] states = new bool[5];
                    int j = 0;
                    for (int i = 0; i < rooms.Length; i = i + 2)
                    {
                        states[j] = rooms[i + 1] == "TRUE";
                        j++;
                    }
                    states[4 ] = rule_parts[3]=="TRUE";

                    lightrule = new Rule(id, states, time);                   

                    rulesList.Add(lightrule);                    
                }
            }

            return rulesList;
        }

        public void AddRule(Rule rule)
        {
            string ruleString = "";
            ruleString += rule.ID.ToString().ToUpper();
            ruleString += "\\" + rule.Time + "\\";
                        
            bool[] states = rule.Body as bool[];
            if (states[0])
                ruleString += "KITCHEN/TRUE/";
            else
                ruleString += "KITCHEN/FALSE/";
            if (states[1])
                ruleString += "LIVING ROOM/TRUE/";
            else
                ruleString += "LIVING ROOM/FALSE/";
            if (states[2])
                ruleString += "BEDROOM/TRUE/";
            else
                ruleString += "BEDROOM/FALSE/";
            if (states[3])
                ruleString += "BATHROOM/TRUE";
            else
                ruleString += "BATHROOM/FALSE";

            ruleString += "\\" + states[4].ToString().ToUpper();

            ConnectionManager.GetInstance().AddLightRules(ruleString);           
        }

        public void DeleteRule(string guid)
        {
            ConnectionManager.GetInstance().DeleteLightRule(guid);
        }
    }
}
